﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dami;
public partial class admin_Modules_quangcao_Edit : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
       // this.btnCancel.Attributes.Add("onclick", "return ClearAll()");
        if (!this.IsPostBack) {
            InitItem();
        }
    }
   
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();
        
    }

    private void AddNew()
    {
        String strMess = "";
        int flag = 0;
        try
        {
             int ID=-1;
        
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
        
            WebData db = new WebData();
            DataLibrary.tbl_Quangcao item = new DataLibrary.tbl_Quangcao();
            clsFileUpLoad fileUpload = new clsFileUpLoad();
            String sPath = fileUpload.FileSaveImage("upload/quangcao", this.fulimageProduct);
            if (String.IsNullOrEmpty(sPath))
            {
                sPath = Session["imageQuangcao"].ToString();
            }
            else {
                fileUpload.DeleteFile(Session["imageQuangcao"].ToString());
            }
            Session.Remove("imageQuangcao");
            item.Image = sPath;
            item.IdQuangcao = ID;
            item.IsActive = 1;
            item.OrderId = Convert.ToInt32(txtorderId.Text);
            item.TitleQuangcao = txtTitle.Text;
            item.UrlQuangcao = txtUrl.Text;
            flag = db.tbl_Quangcaos.UpdateOnSubmit(item);

        }
        catch (Exception err)
        {
            strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
        }


        if (flag > 0)
        {
            Response.Redirect(ViewState["URLReferrer"].ToString());
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + Message.Update_err + "');", true);
        }
    }



    private void InitItem()
    {
        int ID=-1;
        try {
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
        }
        catch { }
        ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        WebData db=new WebData();
        DataLibrary.tbl_Quangcao item = db.tbl_Quangcaos.GetItemById(ID);
        
        if (item != null) {
            txtTitle.Text = item.TitleQuangcao;
            txtUrl.Text = item.UrlQuangcao;
            txtorderId.Text = item.OrderId.ToString();
            Session["imageQuangcao"] = item.Image;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }
    protected void btnCancel_Click1(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }
}